package com.heima.home.config;



import com.heima.home.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration // 声明这是一个Spring配置类
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private AuthInterceptor authInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 使用 registry.addInterceptor() 方法注册拦截器
        registry.addInterceptor(authInterceptor)
                // 1. addPathPatterns("/**"):配置拦截规则，'/**' 表示拦截所有请求。
                .addPathPatterns("/api/**")
                // 2. excludePathPatterns(...):配置排除规则，指定哪些路径不被拦截。
                .excludePathPatterns(
                        "/api/users/login",    // 放行登录接口
                        "/api/users/register", // 放行注册接口
                        "/api/properties/list",
                        "/api/properties/**",  // 放行房源详情查询
                        "/api/reviews/findByPropertyId", // 放行房源评论查询
                        "/uploads/**",         // 放行静态资源访问
                        "/error"
                );
    }


    //把以/uploads/开头的图片映射到你项目根目录下的 uploads 文件夹
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + System.getProperty("user.dir") + "/uploads/");
    }
}